package com.lysj.bhp.scheduled.job;


import com.lysj.bhp.scheduled.base.ScheduledContent;
import com.lysj.bhp.scheduled.domain.BitCoinWallet;
import com.lysj.bhp.scheduled.domain.EthereumWallet;
import com.lysj.bhp.scheduled.domain.RechargeRecord;
import com.lysj.bhp.scheduled.domain.User;
import com.lysj.bhp.scheduled.util.BlockChainUtil;
import com.lysj.bhp.scheduled.util.EtherScanUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.persistence.Convert;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.Random;

@Component
public class EtherumJob extends ScheduledContent {

    private static Logger logger = LoggerFactory.getLogger(EtherumJob.class);


    /*第一次10秒后执行，当执行完后0.5秒再执行，保证一秒并发不超过5个*/
    @Scheduled(initialDelay = 10000, fixedDelay = 500)
    public void queryEthereumWallet() {
        logger.info("EthereumJob is Starting ...");
        EthereumWallet ethereumWallet = ethereumWalletRepository.findFirstByOrderByLastQueryTimeAsc();

        if (ethereumWallet == null) {
            return;
        }

        double realBalance = EtherScanUtil.getBalance(ethereumWallet.getAddress());//查询以太坊钱包的可用余额
        if (realBalance < 0) { //
            logger.error("EherscanUtil return -1,ethereumWalletId is " + ethereumWallet.getEthereumWalletId());
            ethereumWallet.setLastQueryTime(System.currentTimeMillis());
            ethereumWalletRepository.save(ethereumWallet); // !!!即使发生错误,也应该继续执行下一次的查询!!!
            return;
        }
        if (BigDecimal.valueOf(realBalance).compareTo(ethereumWallet.getBookKeepBalance()) == 1) { //查询到的余额大于钱包得真实余额(可用余额)，则更新
            logger.info("EtherWallet Balance Increment,Start Record!" + ethereumWallet.getEthereumWalletId());
            BigDecimal increase = BigDecimal.valueOf(realBalance).subtract(ethereumWallet.getBookKeepBalance()); // 增加的余额

            ethereumWallet.setBalance(ethereumWallet.getBalance().add(increase));//更新钱包的总额
            ethereumWallet.setAvailBalance(ethereumWallet.getAvailBalance().add(increase));//更新钱包的可用余额
            // 创建充值记录
            RechargeRecord rechargeRecord = new RechargeRecord();
            rechargeRecord.setCoinType(20);
            rechargeRecord.setPayAmount(increase);
            rechargeRecord.setUserId(ethereumWallet.getUserId());
            rechargeRecord.setCreateTime(new Date());
            rechargeRecord.setAuthentication("成功");
            rechargeRecord.setName("ETH");
            rechargeRecord.setChineseName("以太坊");
            rechargeRecord.setAddress("0x" + ethereumWallet.getAddress());
            rechargeRecordRepository.save(rechargeRecord);
        }
        ethereumWallet.setBookKeepBalance(BigDecimal.valueOf(realBalance));//更新钱包的记账余额
        ethereumWallet.setLastQueryTime(System.currentTimeMillis()); // 将最近的查询时间设置为现在
        ethereumWalletRepository.save(ethereumWallet);
    }


}
